<template>
  <Layer :layer="layer" @confirm="submit" ref="layerDom">
    <el-form :model="ruleForm" :rules="rules" ref="form" label-width="120px" style="margin-right:30px;">
      <el-form-item label="账号：" prop="userName">
        <el-input v-model="ruleForm.userName" placeholder="请输入账号" :disabled="layer.row != null"></el-input>
      </el-form-item>
      <el-form-item label="姓名：" prop="userTrueName">
        <el-input v-model="ruleForm.userTrueName" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="密码：" prop="password" v-if="layer.row == null">
        <el-input v-model="ruleForm.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="性别：" prop="gender">
        <el-radio-group v-model="ruleForm.gender">
          <el-radio :label="0">女</el-radio>
          <el-radio :label="1">男</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="角色：" prop="role_Id" v-permission="['超级管理员']">
        <el-select v-model="ruleForm.role_Id" placeholder="请选择角色" clearable>
          <el-option v-for="item in roleData" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门：" prop="department_Id" v-permission="['超级管理员']">
        <el-select v-model="ruleForm.department_Id" placeholder="请选择部门" clearable>
          <el-option v-for="item in departmentData" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="电话：" prop="tel">
        <el-input v-model="ruleForm.tel" placeholder="请输入电话"></el-input>
      </el-form-item>
      <el-form-item label="邮箱：" prop="email">
        <el-input v-model="ruleForm.email" placeholder="请输入邮箱"></el-input>
      </el-form-item>
      <el-form-item label="出生日期：" prop="birthDate">
        <el-date-picker v-model="ruleForm.birthDate" type="date" placeholder="请选择出生日期" />
      </el-form-item>
    </el-form>
  </Layer>
</template>

<script lang="ts">
import { defineComponent, ref, reactive } from 'vue'
import { roleData, departmentData } from './enum'
import Layer from '@/components/layer/index.vue'
import { add, update } from '@/api/system/user'
import Permission from '@/directive/permission'
export default defineComponent({
  components: {
    Layer
  },
  directives: {
    Permission
  },
  props: {
    layer: {
      type: Object,
      default: () => {
        return {
          show: false,
          title: '',
          showButton: true
        }
      }
    }
  },
  setup(props, ctx) {
    const layerDom: Ref<LayerType | null> = ref(null)
    let ruleForm = ref({
      userName: '',
      userTrueName: '',
      password: '',
      gender: 0,
      department_Id: '',
      role_Id: '',
      tel:'',
      email:'',
      birthDate:''
    })
    const rules = {
      userName: [{ required: true, message: '请输入账号', trigger: 'blur' }],
      password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
    }
    init()
    function init() { // 用于判断新增还是编辑功能
      if (props.layer.row) {
        ruleForm.value = JSON.parse(JSON.stringify(props.layer.row)) // 数量量少的直接使用这个转
        if(ruleForm.value.birthDate) ruleForm.value.birthDate = new Date(ruleForm.value.birthDate)
      }
    }
    return {
      ruleForm,
      rules,
      layerDom,
      roleData,
      departmentData
    }
  },
  methods: {
    submit() {
      this.$refs['form'].validate((valid: boolean) => {
        if (valid) {
          let params = this.ruleForm

          if (this.layer.row) {
            this.updateForm(params)
          } else {
            this.addForm(params)
          }
        } else {
          return false;
        }
      });
    },
    // 新增提交事件
    addForm(params: object) {
      add(params)
        .then(res => {
          this.$message({
            type: 'success',
            message: '新增成功'
          })
          this.layer.show = false
          this.$emit('getTableData', true)
          this.layerDom && this.layerDom.close()
        })
    },
    // 编辑提交事件
    updateForm(params: object) {
      update(params)
        .then(res => {
          this.$message({
            type: 'success',
            message: '编辑成功'
          })
          this.$emit('getTableData', false)
          this.layerDom && this.layerDom.close()
        })
    }
  }
})
</script>

<style lang="scss" scoped></style>